﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Loader
{
    /// <summary>
    /// Interaction logic for patientListItem.xaml
    /// </summary>
    public partial class patientListItem : UserControl
    {
        int countComplete = 0;
        public patientListItem()
        {
            InitializeComponent();
        }
        public void resetCount()
        {
            count = 0;
        }
        //static bool isTime = true;
        static Queue<BackgroundWorker> queue = new Queue<BackgroundWorker>();
        public static int count = 0;
        private int totalCount = 0;
        private Window2 win;
        ServiceReference2.Service1Client svc = new ServiceReference2.Service1Client();
        public patient selectedPatient;
        bool mypatient;
        public patientListItem(patient p, Window2 win, int totalCount, bool mypatient)
        {
            InitializeComponent();
            this.totalCount = totalCount;
            selectedPatient = p;
            this.win = win;
            this.mypatient = mypatient;
            int age = new DateTime(DateTime.Now.Subtract(p.DateOfBirth).Ticks).Year - 1;

            DOBValue.Text = String.Concat(p.DateOfBirth.ToShortDateString(), " (", age.ToString(), ")");
            nameLabel1.FamilyName = p.LastName;
            if (p.Gender.ToLower().Equals("m"))
            {
                GenderValue.Text = Microsoft.Cui.Controls.PatientGender.Male.ToString();
            }
            else if (p.Gender.ToLower().Equals("f"))
            {
                GenderValue.Text = Microsoft.Cui.Controls.PatientGender.Female.ToString();
            }

            nameLabel1.GivenName = String.Concat(p.FirstName);
            nameLabel1.Name = p.FirstName;
            nameLabel1.Title = p.Title;
            IdentifierValue.Text = p.Uid.ToString();
            //contactLabel1.WorkPhoneNumber = p.ContactNumWork;
            //lblEname1.Text = p.E_name;
            //lblEContact1.Text = p.E_contactNum;
            //lblErelation1.Text = p.E_relationship;

            // new method
            svc.retrieveExeAssignedtoPatientCompleted += new EventHandler<ServiceReference2.retrieveExeAssignedtoPatientCompletedEventArgs>(svc_retrieveExeAssignedtoPatientCompleted);
            svc.retrieveSessionOfPatientCompleted += new EventHandler<ServiceReference2.retrieveSessionOfPatientCompletedEventArgs>(svc_retrieveSessionOfPatientCompleted);
            svc.retrievePatientProblemsCompleted += new EventHandler<ServiceReference2.retrievePatientProblemsCompletedEventArgs>(svc_retrievePatientProblemsCompleted);
            if (p.Uid == 1)
            {
                svc.retrieveSessionOfPatientAsync(selectedPatient.Uid);
                svc.retrieveExeAssignedtoPatientAsync(selectedPatient.Uid);
            }
            else
            {
                countComplete++;
                countComplete++;
            }
            svc.retrievePatientProblemsAsync(selectedPatient.Uid);
           

            //BackgroundWorker _backgroundWorker = new BackgroundWorker();

            //// Set up the Background Worker Events
            //_backgroundWorker.DoWork += _backgroundWorker_DoWork;
            //_backgroundWorker.RunWorkerCompleted += _backgroundWorker_RunWorkerCompleted;

            //queue.Enqueue(_backgroundWorker);
            //// Run the Background Worker
            //if (isTime == true)
            //{
            //    isTime = false;
            //    _backgroundWorker.RunWorkerAsync(6000);

            //}

            //svc.retrieveExeAssignedtoPatientCompleted += new EventHandler<ServiceReference2.retrieveExeAssignedtoPatientCompletedEventArgs>(svc_retrieveExeAssignedtoPatientCompleted);
            //svc.retrieveExeAssignedtoPatientAsync(p.Uid);


            //// retrieve all treatment analysis data


            ////retrieve problems that the patient have



        }

        void svc_retrievePatientProblemsCompleted(object sender, ServiceReference2.retrievePatientProblemsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                problemList = e.Result;
                if (countComplete != 2)
                {
                    countComplete++;
                }
                else
                {
                    Dispatcher.BeginInvoke((Action)delegate
                    {
                        win.preDownloadPart2(selectedPatient, exeAssignedList, patientSessionList, problemList, mypatient);
                    });
                    count++;
                    if (count == totalCount)
                    {
                        this.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            win.preDownloadPart3();
                            // stop loading bar
                            win.stopLoading();
                        }));
                    }
                }
            }
        }

        void svc_retrieveSessionOfPatientCompleted(object sender, ServiceReference2.retrieveSessionOfPatientCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                patientSessionList = e.Result;
                if (countComplete != 2)
                {
                    countComplete++;
                }
                else
                {
                    Dispatcher.BeginInvoke((Action)delegate
                    {
                        win.preDownloadPart2(selectedPatient, exeAssignedList, patientSessionList, problemList, mypatient);
                    });
                    count++;
                    if (count == totalCount)
                    {
                        this.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            win.preDownloadPart3();
                            // stop loading bar
                            win.stopLoading();
                        }));


                    }
                }
            }
        }

        void svc_retrieveExeAssignedtoPatientCompleted(object sender, ServiceReference2.retrieveExeAssignedtoPatientCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                exeAssignedList = e.Result;
                if (countComplete != 2)
                {
                    countComplete++;
                }
                else
                {
                    Dispatcher.BeginInvoke((Action)delegate
                    {
                        win.preDownloadPart2(selectedPatient, exeAssignedList, patientSessionList, problemList, mypatient);
                    });
                    count++;
                    if (count == totalCount)
                    {
                        this.Dispatcher.BeginInvoke((Action)(() =>
                        {
                            win.preDownloadPart3();
                            // stop loading bar
                            win.stopLoading();
                        }));


                    }
                }
            }
        }

        // Completed Method
        void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //old method
            queue.Dequeue();
            if (queue.Count > 0)
            {
                BackgroundWorker nextItem = queue.Peek();
                nextItem.RunWorkerAsync(5000);
            }
            //else
            //{
            //    win.preDownloadPart3();
            //    // stop loading bar
            //    win.stopLoading();
            //}

        }


        public List<ServiceReference2.EXE> exeAssignedList = new List<ServiceReference2.EXE>();
        public List<ServiceReference2.session> patientSessionList = new List<ServiceReference2.session>();
        public List<ServiceReference2.problem> problemList = new List<ServiceReference2.problem>();
    }
}
